package com.css.crm.service.impl;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.css.crm.dao.SystemDao;
import com.css.crm.dao.impl.SystemDaoImpl;
import com.css.crm.exception.SystemException;
import com.css.crm.message.SystemErrorMessage;
import com.css.crm.service.SystemService;
import com.css.crm.vo.DiscountCodeVo;
import com.css.crm.vo.DepartmentVo;
import com.css.crm.vo.PlaceVo;
import com.css.crm.vo.StaticReferenceVo;
import com.css.crm.vo.MenuMap;
import com.css.crm.vo.ObjectPermission;
import com.css.crm.vo.TitleVo;
import com.css.crm.vo.SystemMenu;
import com.css.crm.vo.User;

/**
 * Implementation class for SystemService
 */
/*
 * Log Date Name Description 0024746 2012 DEC 13 lawrence.ng.c.k Creation
 */
public class SystemServiceImpl extends BaseServiceImpl implements SystemService {

	private SystemDao sDAO = null;

	public SystemServiceImpl() {
		sDAO = new SystemDaoImpl();
	}

	@Override
	protected void initDaoSession() {
		sDAO.setMyBatisSession(getSession());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.css.crm.service.SystemService#getApplTopMenu()
	 */
	public List<SystemMenu> getApplTopMenu() throws SystemException {
		try {
			return sDAO.getSystemMenu();
		} catch (Exception e) {
			throw new SystemException(SystemErrorMessage.SQL_UNEXPECTED_EXCEPTION, e);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.css.crm.service.SystemService#getHkwhMenuMap()
	 */
	public MenuMap getCrmMenuMap() throws SystemException {
		try {
			return sDAO.getCrmMenuMap();
		} catch (Exception e) {
			throw new SystemException(SystemErrorMessage.SQL_UNEXPECTED_EXCEPTION, e);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.css.crm.service.SystemService#getApplAccount(java.lang.String,
	 * java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	public List<User> getApplAccount(String ldapId, String applId) throws SystemException {
		try {
			return (List<User>) sDAO.getApplAccount(ldapId, applId);
		} catch (Exception e) {
			throw new SystemException(SystemErrorMessage.SQL_UNEXPECTED_EXCEPTION, e);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.css.crm.service.SystemService#getAllObjectPermission(java.lang.String
	 * , java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	public List<ObjectPermission> getAllObjectPermission(String accountId, String applId) throws SystemException {
		try {
			return (List<ObjectPermission>) sDAO.getAllObjectPermission(accountId, applId);
		} catch (Exception e) {
			throw new SystemException(SystemErrorMessage.SQL_UNEXPECTED_EXCEPTION, e);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.css.crm.service.SystemService#getAccountUnits(java.lang.String,
	 * java.lang.String)
	 */
	public String[] getAccountUnits(String accountId, String applId) throws SystemException {
		try {
			return sDAO.getAccountUnits(accountId, applId);
		} catch (Exception e) {
			throw new SystemException(SystemErrorMessage.SQL_UNEXPECTED_EXCEPTION, e);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.css.crm.service.SystemService#getAccountDefaultUnit(java.lang.String,
	 * java.lang.String)
	 */
	public String getAccountDefaultUnit(String accountId, String applId) throws SystemException {
		try {
			return sDAO.getAccountDefaultUnit(accountId, applId);
		} catch (SystemException e) {
			throw e;
		} catch (Exception e) {
			throw new SystemException(SystemErrorMessage.SQL_UNEXPECTED_EXCEPTION, e);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.css.crm.service.SystemService#getAccountSingleUnit(java.lang.String,
	 * java.lang.String)
	 */
	public String getAccountSingleUnit(String accountId, String applId) throws SystemException {
		try {
			return sDAO.getAccountSingleUnit(accountId, applId);
		} catch (SystemException e) {
			throw e;
		} catch (Exception e) {
			throw new SystemException(SystemErrorMessage.SQL_UNEXPECTED_EXCEPTION, e);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.css.crm.service.SystemService#getOrigCtryCode(java.lang.String)
	 */
	@Override
	public Long getOrigCtryCode(String deptCde) throws SystemException {
		try {
			String ctryCde = sDAO.getOrigCtryCode(deptCde);
			return StringUtils.isEmpty(ctryCde) ? null : new Long(ctryCde);
		} catch (Exception e) {
			throw new SystemException(SystemErrorMessage.SQL_UNEXPECTED_EXCEPTION, e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.css.crm.service.SystemService#loadStaticReference(com.css.crm.vo.StaticReferenceVo
	 * )
	 */
	@Override
	public List<StaticReferenceVo> loadStaticReference(StaticReferenceVo staticReference) throws SystemException {
		try {
			return sDAO.getStaticReference(staticReference);
		} catch (Exception e) {
			throw new SystemException(SystemErrorMessage.SQL_UNEXPECTED_EXCEPTION, e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.css.crm.service.SystemService#loadPsStatcRefExt(com.css.crm.vo.TitleVo
	 * )
	 */
	@Override
	public List<TitleVo> loadTitle(TitleVo title) throws SystemException {
		try {
			return sDAO.getPsStatcRefExt(title);
		} catch (Exception e) {
			throw new SystemException(SystemErrorMessage.SQL_UNEXPECTED_EXCEPTION, e);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.css.crm.service.SystemService#loadPsDisc(com.css.crm.vo.DiscountCodeVo
	 * )
	 */
	@Override
	public List<DiscountCodeVo> loadPsDisc(DiscountCodeVo vo) throws SystemException {
		try {
			return sDAO.getPsDisc(vo);
		} catch (Exception e) {
			throw new SystemException(SystemErrorMessage.SQL_UNEXPECTED_EXCEPTION, e);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.css.crm.service.SystemService#loadPlace()
	 */
	@Override
	public List<PlaceVo> loadPlace() throws SystemException {
		try {
			return sDAO.getPlace(null);
		} catch (Exception e) {
			throw new SystemException(SystemErrorMessage.SQL_UNEXPECTED_EXCEPTION, e);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.css.crm.service.SystemService#loadDepartment()
	 */
	@Override
	public List<DepartmentVo> loadDepartment() throws SystemException {
		try {
			return sDAO.getDepartment(null);
		} catch (Exception e) {
			throw new SystemException(SystemErrorMessage.SQL_UNEXPECTED_EXCEPTION, e);
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.css.crm.service.SystemService#getUserIdByLdapId(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public String getUserIdByLdapId(String ldapUserId, String userUnitCde) throws SystemException {
		try {
			return sDAO.getSmUserId(ldapUserId, userUnitCde);
		} catch (Exception e) {
			throw new SystemException(SystemErrorMessage.SQL_UNEXPECTED_EXCEPTION, e);
		}
	}
}
